package com.jobpass.controller;

import com.jobpass.common.data.ResultData;
import com.jobpass.common.model.Sign;
import com.jobpass.common.service.SignService;
import com.jobpass.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Calendar;

/**
 * @Author: Qning
 * @Description: 签到
 * @Date: 2020/7/19 10:36
 * @PackageName: com.jobpass.controller
 * @ClassName: SignController
 */
@Api(tags="签到Controller")
@RestController
@RequestMapping("student")
public class SignController {
    @Resource
    private SignService signService;

    //用户ID
    private static final long studentId=2;
    private static final int isState=1;

    /**
     *
     * @Author: Qning
     * @Description: 签到
     * @Date: 22:45 2020/7/20
     * @param
     * @return: com.jobpass.common.data.ResultData
     */
    @ApiOperation(value="签到")
    @PostMapping("/sign")
    public ResultData sign(){
        //@RequestBody Sign sign
        Sign sign = new Sign();
        sign.setStudentId(studentId);
        sign.setIsState(isState);
        sign.setSite("西安");

        String key=studentId+"sign"+isState;
        Object o = RedisUtil.get(key);

        //今天是否签到
        if (o == null) {
            //未签到
            int savaSign = signService.SavaSign(sign);
            if (savaSign == 1) {
                //存入签到用户信息
                RedisUtil.set(key,"sign",getRefreshTime());
                return  ResultData.success("签到成功");
            }else {
                return ResultData.fail("1","签到失败");
            }
        }else {
            //已签到
            return ResultData.fail("1","已签到");
        }

    }


    /**
     * 获取当前时间离明天凌晨还有多少小时
     * @return
     */
    public static int getRefreshTime(){
        Calendar calendar = Calendar.getInstance();
        int now = (int) (calendar.getTimeInMillis()/1000);
        calendar.add(Calendar.DATE, 1);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY , 0);
        return (int) (calendar.getTimeInMillis()/1000-now);
    }
}
